package com.cr.dataprotect.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.AnyTypePermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * XML工具类 - 使用XStream实现XML与Java对象互转
 */
public class XmlUtil {
    private static final Logger log = LoggerFactory.getLogger(XmlUtil.class);

    private XmlUtil() {
    }

    public static <T> T xmlToEntity(String content, Class<T> nodeClass) {
        XStream xStream = new XStream();
        xStream.addPermission(AnyTypePermission.ANY);
        xStream.processAnnotations(nodeClass);
        xStream.autodetectAnnotations(true);
        return (T)xStream.fromXML(content);
    }

    public static <T> String entityToXml(T bean) {
        XStream xStream = new XStream();
        if (bean == null) {
            return "";
        } else {
            xStream.processAnnotations(bean.getClass());
            xStream.autodetectAnnotations(true);
            return xStream.toXML(bean);
        }
    }
}